/*
 * Copyright 2009 Jiwei Xu (http://www.xujiwei.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

provide("TinyJS.TinyWin");

TinyWin = Class({
    Initialize : function(config) {
        config = config || {};
        this.Text = config.Text || "";
        this.ExtraClass = config.ExtraClass || "";
        this.Elements = {
            Window : null,
            Head : null,
            Body : null,
            Foot : null
        };
        this.Body = this.Elements.Body;

        if (config.Width) {
            this.Width = config.Width;
        }

        if (config.Height) {
            this.Height = config.Height;
        }

        this.MinButton = config.MinButton || true;
        this.ExpandButton = config.ExpandButton || true;
        this.Visible = true;
        this.Resizable = config.Resizable || true;
    },

    Show : function(show) {
        var el = this.Elements.Window;
        if (show !== undefined) {
            console.log(el.style.display);
        } else {
            el.style.display = window.getComputedStyle(el, null).display == "block" ? "none" : "block";
        }
    },

    Expand : function(expand) {
        var el = this.Elements.Body;
        if (expand !== undefined) {
            el.style.display = expand ? "block" : "none";
        } else {
            el.style.display = window.getComputedStyle(el, null).display == "block" ? "none" : "block";
        }
    },

    Create : function(container) {
        var win = document.createElement("div");
        win.className = "box";
        win.className += this.ExtraClass ? " " + this.ExtraClass : "";

        if (this.Width > 0) {
            win.style.width = this.Width + "px";
        }

        if (this.Height > 0) {
            win.style.height = this.Height + "px";
        }

        this.Elements.Window = win;
        this.Elements.Head = this.__CreateHead();
        this.Elements.Body = this.__CreateBody();
        this.Elements.Foot = this.__CreateFoot();
        this.Body = this.Elements.Body;

        win.appendChild(this.Elements.Head);
        win.appendChild(this.Elements.Body);
        win.appendChild(this.Elements.Foot);

        (container || document.body).appendChild(win);

        $(win).draggable({handle : "h5"});
        if (this.Resizable) {
            $(win).resizable();
        }
    },

    __CreateHead : function() {
        var div = document.createElement("div");
        div.className = "hd";

        var node = this.CreateHead();
        if (node) {
            if (node.nodeType) {
                div.appendChild(node);
            } else if (node.Element) {
                div.appendChild(node.Element);
            } else if (typeof node === "string") {
                div.innerHTML = node;
            }
        }

        var el, THIS = this;
        if (this.ExpandButton) {
            el = document.createElement("span");
            el.className = "btn expand";
            el.innerHTML = "-";
            el.onclick = function() {
                THIS.Expand();
            };
            div.appendChild(el);
        }

        if (this.MinButton) {
            el = document.createElement("span");
            el.className = "btn min";
            el.innerHTML = "X";
            el.onclick = function() {
                THIS.Show();
            };
            div.appendChild(el);
        }

        return div;
    },

    __CreateBody : function() {
        var div = document.createElement("div");
        div.className = "bd";

        var node = this.CreateBody();
        if (node) {
            if (node.nodeType) {
                div.appendChild(node);
            } else if (node.Element) {
                div.appendChild(node.Element);
            } else if (typeof node === "string") {
                div.innerHTML = node;
            }
        }

        return div;
    },

    __CreateFoot : function() {
        var div = document.createElement("div");
        div.className = "ft";

        var node = this.CreateFoot();
        if (node) {
            if (node.nodeType) {
                div.appendChild(node);
            } else if (node.Element) {
                div.appendChild(node.Element);
            } else if (typeof node === "string") {
                div.innerHTML = node;
            }
        }

        return div;
    },

    CreateHead : function() {
        return "<h5>" + this.Text + "</h5>";
    },

    CreateBody : function() {
        return "";
    },

    CreateFoot : function() {
        return "";
    }
});